package com.xcx.service.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcx.dto.StoreInspectionDetailDto;
import com.xcx.entity.StoreInspectionDetail;
import com.xcx.exception.BaseException;
import com.xcx.mapper.StoreInspectionDetailMapper;
import com.xcx.utils.SQLUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mwf
 */
@Slf4j
@Service
public class StoreInspectionDetailServiceImpl extends ServiceImpl<StoreInspectionDetailMapper, StoreInspectionDetail> {

    @Autowired
    private StoreInspectionDetailMapper storeInspectionDetailMapper;
    @Autowired
    private SQLUtils sqlUtils;

    public int additem(StoreInspectionDetailDto storeInspectionDetailDto) {
        StoreInspectionDetail storeInspectionDetail = new StoreInspectionDetail();
        BeanUtils.copyProperties(storeInspectionDetailDto,storeInspectionDetail);
        storeInspectionDetail.setCreateDate(LocalDateTime.now());
        int insert = storeInspectionDetailMapper.insert(storeInspectionDetail);
        if (insert != 1){
            throw new BaseException("添加失败!");
        }
        return insert;
    }

    public int updateitem(StoreInspectionDetailDto storeInspectionDetailDto) {
        StoreInspectionDetail storeInspectionDetail = new StoreInspectionDetail();
        BeanUtils.copyProperties(storeInspectionDetailDto,storeInspectionDetail);
        int i = storeInspectionDetailMapper.updateById(storeInspectionDetail);
        if (i != 1) throw new BaseException("修改失败!");
        return i;
    }

    public List<StoreInspectionDetail> queryitemByIdentity(Map<String, Object> map) {
        Map<String, Object> preprocessedParams = new HashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String newKey = StringUtils.camelToUnderline (entry.getKey());
            preprocessedParams.put(newKey, entry.getValue());
        }
        List<StoreInspectionDetail> storeInspectionDetails = storeInspectionDetailMapper.selectByMap(preprocessedParams);
        return storeInspectionDetails;
    }


    public JSONArray getSubUser(String subbh) {
        String sql = "select code,name from org_employees where departcode = "+subbh+" and state = '0'";
        String query = sqlUtils.sendSql(sql, "query");
        JSONArray jsonArray = JSON.parseArray(query);
        return jsonArray;
    }


    public JSONArray getSubDetail(String subbh) {
        String sql = "select a.name subname,a.parentname,b.code,b.name empName,b.dingzw from org_departs a join org_employees b on a.code = '"+subbh+"' and a.code = b.departcode and b.state = 0 ";
        String query = sqlUtils.sendSql(sql, "query");
        JSONArray jsonArray = JSON.parseArray(query);
        return jsonArray;
    }
}
